package org.jsola.contract.service.impl;

import org.jsola.contract.common.ExampleKit;
import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dao.ICompanyContractShowDAO;
import org.jsola.contract.dto.CompanyContractShowAddDTO;
import org.jsola.contract.dto.CompanyContractShowUpdateDTO;
import org.jsola.contract.entity.CompanyContractShowDO;
import org.jsola.contract.query.CompanyContractShowQuery;
import org.jsola.contract.service.ICompanyContractShowService;
import org.jsola.contract.vo.CompanyContractShowListVO;
import org.jsola.contract.vo.CompanyContractShowVO;
import org.jsola.core.Page;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 合同员工签署后是否展示
 *
 * @author JanusMix
 */
@Service("contractCompanyContractShowServiceImpl")
public class CompanyContractShowServiceImpl implements ICompanyContractShowService {

    @Autowired
    private ICompanyContractShowDAO companyContractShowDAO;

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public CompanyContractShowVO save(CompanyContractShowAddDTO companyContractShowAddDTO, TokenUser tokenUser) {
        // 转DO
        CompanyContractShowDO companyContractShowDO = companyContractShowAddDTO.to(CompanyContractShowDO.class);
        // 保存
        companyContractShowDO = save(companyContractShowDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return companyContractShowDO.to(CompanyContractShowVO.class);
    }

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public int update(CompanyContractShowUpdateDTO companyContractShowUpdateDTO, TokenUser tokenUser) {
        //转DO
        CompanyContractShowDO companyContractShowDO = companyContractShowUpdateDTO.to(CompanyContractShowDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(companyContractShowDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... companyContractShowIds) {
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) companyContractShowIds);
    }


    @Override
    public CompanyContractShowVO selectById(String companyContractShowId, String siteId) {
        CompanyContractShowDO companyContractShowDO = selectDOById(companyContractShowId, siteId);
        if (companyContractShowDO == null) {
            return null;
        }
        return companyContractShowDO.to(CompanyContractShowVO.class);
    }

    @Override
    public List<CompanyContractShowListVO> select(CompanyContractShowQuery companyContractShowQuery, String siteId) {
        List<CompanyContractShowDO> companyContractShowDOList = selectDO(companyContractShowQuery, siteId);
        if(CollectionUtils.isEmpty(companyContractShowDOList)) {
            return companyContractShowDOList == null ? null : new ArrayList<>();
        }
        return companyContractShowDOList.stream()
                .map(companyContractShowDO -> companyContractShowDO.to(CompanyContractShowListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(CompanyContractShowQuery companyContractShowQuery, String siteId) {
        Example example = buildExample(companyContractShowQuery,siteId);
        return companyContractShowDAO.selectCountByExample(example);
    }

    @Override
    public Page<CompanyContractShowListVO> selectPage(CompanyContractShowQuery companyContractShowQuery, String siteId) {
        Example example = buildExample(companyContractShowQuery,siteId);
        Page<CompanyContractShowDO> page = companyContractShowDAO.selectPageByExample(example,
                companyContractShowQuery.getPageNo(),
                companyContractShowQuery.getPageSize());

        return page.to(CompanyContractShowListVO.class);
    }

    @Override
    public CompanyContractShowDO selectDOById(String companyContractShowId, String siteId) {
        return listById(companyContractShowId, siteId);
    }

    @Override
    public List<CompanyContractShowDO> selectDO(CompanyContractShowQuery companyContractShowQuery, String siteId) {
        Example example = buildExample(companyContractShowQuery,siteId);
        return companyContractShowDAO.selectByExample(example);
    }

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public CompanyContractShowVO findByCompanyId(String companyId, TokenUser tokenUser) {
        Example example = new Example(CompanyContractShowDO.class);
        example.and()
                .andEqualTo("companyId", companyId)
                .andEqualTo("siteId", tokenUser.getSiteId());

        CompanyContractShowDO showDO = companyContractShowDAO.selectOneByExample(example);
        if (Objects.isNull(showDO)) {
            // 没有新增一条
            CompanyContractShowAddDTO showAddDTO = new CompanyContractShowAddDTO();
            showAddDTO.setCompanyId(companyId);
            showAddDTO.setIsShow(true);
            return save(showAddDTO, tokenUser);
        }
        return showDO.to(CompanyContractShowVO.class);
    }

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public void updateCompanyShow(String companyId, Boolean isShow, TokenUser tokenUser) {
        Example example = new Example(CompanyContractShowDO.class);
        example.and()
                .andEqualTo("companyId", companyId)
                .andEqualTo("siteId", tokenUser.getSiteId());

        CompanyContractShowDO showDO = companyContractShowDAO.selectOneByExample(example);
        if (Objects.isNull(showDO)) {
            // 没有新增一条
            CompanyContractShowAddDTO showAddDTO = new CompanyContractShowAddDTO();
            showAddDTO.setCompanyId(companyId);
            showAddDTO.setIsShow(isShow);
            save(showAddDTO, tokenUser);
        } else {
            // 有就修改
            CompanyContractShowUpdateDTO showUpdateDTO = new CompanyContractShowUpdateDTO();
            showUpdateDTO.setId(showDO.getId());
            showUpdateDTO.setCompanyId(companyId);
            showUpdateDTO.setIsShow(isShow);
            update(showUpdateDTO, tokenUser);
        }
    }


    /**
     * 根据查询参数，构建example

     * @param companyContractShowQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(CompanyContractShowQuery companyContractShowQuery, String siteId) {
        Example example = new Example(CompanyContractShowDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        if (companyContractShowQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", companyContractShowQuery.getCompanyId());
        }
        if (companyContractShowQuery.getIsShow() != null) {
            example.and().andEqualTo("isShow", companyContractShowQuery.getIsShow());
        }
        // 排序
        ExampleKit.setExampleOrder(example,companyContractShowQuery.getOrders());
        return example;
    }
}




